<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\CarPackageUsedLog;
use App\Admin\Services\AdminUserService;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Models\Administrator;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Auth;

class CarPackageOperatorLogController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $request = request();

        return Grid::make(CarPackageUsedLog::with(['order', 'store', 'operator', 'attr']), function (Grid $grid) use ($request) {
            if ($request->has('operator_user_id')) {
                $grid->model()->where('operator_user_id', $request->get('operator_user_id'));
            }

            $package_id = AdminUserService::permissionOrders(Auth::guard('admin')->user()->username);

            if ($package_id) {
                $grid->model()->whereIn('package_id', $package_id);
            }

            $grid->column('order_id', admin_trans('car-package-used-log.fields.order_id'));
            $grid->column('operator.name', admin_trans('car-package-used-log.fields.operator_user_id'));
            $grid->column('order.user_name', admin_trans('car-package-used-log.fields.user_id'));
            $grid->column('order.user_phone', admin_trans('car-package-used-log.fields.user_phone'));
            $grid->column('attr.attr_title', admin_trans('car-package-used-log.fields.attr_id'));
            $grid->column('status')->using(admin_trans('car-package-used-log.options.status'))->badge([
                0 => 'primary',
                1 => 'success',
            ]);
            $grid->column('operator_at');
            $grid->export();
            $grid->model()->orderBy('id', 'desc');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like('operatorUser.name', admin_trans('car-package-used-log.fields.operator_user_id'));
                $filter->like('user.name', admin_trans('car-package-used-log.fields.user_id'));
                $filter->like('order.user_phone', admin_trans('car-package-used-log.fields.user_phone'));
                $filter->like('user.car_number', admin_trans('car-package-used-log.fields.car_number'));
                $filter->like('order.order_sn', admin_trans('car-package-used-log.fields.order_id'));
                $filter->equal('status')->select(admin_trans('car-package-used-log.options.status'));
                $filter->between('operator_at')->datetime();
            });

            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->disableCreateButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, CarPackageUsedLog::with(['order', 'store', 'operator']), function (Show $show) {
            $show->field('id');
            $show->field('order.user_name', admin_trans('car-package-order-attr.fields.user_id'));
            $show->field('order.user_phone', admin_trans('car-package-used-log.fields.user_phone'));
            $show->field('operator.name', admin_trans('car-package-used-log.fields.operator_user_id'));
            $show->field('operator_pics')->unescape()->as(function ($operator_pics) {
                $json_arr = json_decode($operator_pics, true);
                $str = '';

                if ($json_arr) {
                    foreach ($json_arr as $key => $value) {
                        $str .=  "<img width=200 src='".$json_arr[0]['url']."' />";
                    }
                }

                return $str;
            });
            $show->field('remarks');
            $show->field('used_at');
            $show->field('operator_at');
            $show->field('created_at');
            $show->field('updated_at');

            $show->panel()->tools(function ($tools) {
                $tools->disableDelete();
                $tools->disableEdit();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CarPackageUsedLog(), function (Form $form) {
            $form->display('id');
            $form->text('order_id');
            $form->text('package_id');
            $form->text('package_title');
            $form->text('package_pic');
            $form->text('used_at');
            $form->text('store_id');
            $form->text('operator_user_id');
            $form->text('operator_pics');
            $form->text('operator_at');
            $form->text('car_number');
            $form->text('remarks');
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
